package com.lcf.app.basic.async;

import com.lcf.app.basic.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author ChenFei
 * @date 2021/11/11
 */
@Slf4j
@Service
public class AsyncService {

    private static Map<String, User> userMap;

    static {
        userMap = Arrays.asList(new User("LiSi", 18, "110"), new User("ZhangSan", 24, "119"))
                .stream()
                .collect(Collectors.toMap(User::getName, Function.identity()));
    }

    @Async
    public void method1() {
        try {
            log.info("开始执行method1()。。。");
            Thread.sleep(3000);
        } catch (Exception e) {

        }
        log.info("method1()执行完毕。。。");
    }

    @Async
    public CompletableFuture<User> method2(String name) throws InterruptedException {
        Thread.sleep(5000);
        return CompletableFuture.completedFuture(userMap.get(name));
    }

    @Async
    public CompletableFuture<User> method3(String name) throws InterruptedException {
        Thread.sleep(3000);
        return CompletableFuture.completedFuture(userMap.get(name));
    }

}
